<?php
	/**
	*栏目管理类
	**/
	class CategoryClass {
		// 构造函数
		public function __constract(){
		}

		// 获取栏目列表
		public function getList($data){
			global $db;

			// 获取所有数据
			$db->query("select
							cat.id,
							cat.name,
							cat.create_time as createTime,
							cat.parent_id as parentId,
							cat.type,
							model.name as modelName,
							model.id as modelId,
							cat.listorder,
							cat.description,
							cat.content,
							user.name as createName
						from category as cat
						left join admin_user as user
						on cat.creator=user.id
						left join model
						on cat.modelid= model.id
						order by cat.listorder asc, cat.id asc", "");

			return $db->prepare->fetchAll();
		}

		// 添加栏目
		public function add($data){
			global $db, $session;
			$loginUserId = $session->get("userId");
			$name = $data->name;
			$parentId = $data->parentId;
			$description = $data->description;
			$content = $data->content;
			$modelId = $data->modelId;
			$type = $data->type;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 查询栏目名称是否已经存在
			$db->query("select * 
						from category
						where
							name=:name
						for update",
						array(
							"name"=>$name
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "栏目名称已经存在了，请重新命名！", null);
			}

			// 插入一条新数据
			$db->query("insert into category(
							name,
							parent_id,
							creator,
							description,
							content,
							modelid,
							type) 
						values(
							:name,
							:parentId,
							:creator,
							:description,
							:content,
							:modelid,
							:type
						)", 
						array(
							"name"=>$name,
							"parentId"=>$parentId,
							"creator"=>$loginUserId,
							"description"=>$description,
							"content"=>$content,
							"modelid"=>$modelId,
							"type"=>$type
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}

			$db->PDO->commit();
			// 事物结束
			
			// 删除session中modelToTableName数据
			$session->del('catToTableName');
		}

		// 删除栏目
		public function del($data){
			global $db,$session;
			$id = $data->id;

			// 查询栏目是否含有子栏目
			$db->query("select * 
						from category
						where
							parent_id=:id",
						array(
							"id"=>$id
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				returnAjaxJson(400, "栏目含有子栏目，不能删除！", null);
			}

			// 查询栏目modelid
			$db->query("select modelid as modelId 
						from category
						where
							id=:id",
						array(
							"id"=>$id
							)
						);
			$result = $db->prepare->fetchAll();
			if(!$result){
				returnAjaxJson(400, "id参数有误！", null);
			}
			$modelId = $result[0]['modelId'];

			// 获取数据存储表名称
			$db->query("select table_name as tableName 
						from model
						where
							id=:modelId",
						array(
							"modelId"=>$modelId
							)
						);
			$result = $db->prepare->fetchAll();
			if(!$result){
				returnAjaxJson(400, "id参数有误！", null);
			}
			$tableName = $result[0]['tableName'];

			// 查询栏目里是否有文章
			$sql = "select * from " . $tableName . " where catid=:id";
			$db->query($sql, array(
							"id"=>$id
							));
			$result = $db->prepare->fetchAll();
			if($result){
				returnAjaxJson(400, "栏目有文章，不能删除！", null);
			}

			// 删除category表中的数据
			$db->query("delete from category
						where 
							id=:id",
						array(
							"id"=>$id
							)
						);

			// 删除session中modelToTableName数据
			$session->del('catToTableName');
		}

		// 编辑栏目
		public function edit($data){
			global $db;
			$id = $data->id;
			$name = $data->name;
			$description = $data->description;
			$content = $data->content;

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			// 查询栏目名称是否已经存在
			$db->query("select * 
						from category
						where
							name=:name and
							id!=:id
						for update",
						array(
							"name"=>$name,
							"id"=>$id
							)
						);
			$result = $db->prepare->fetchAll();
			if($result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "栏目名称已经存在了，请重新命名！", null);
			}

			// 更新数据
			$db->query("update category
						set
							name=:name,
							description=:description,
							content=:content
						where
							id=:id",
						array(
							"id"=>$id,
							"name"=>$name,
							"description"=>$description,
							"content"=>$content
							)
						);
			$result = $db->prepare->rowCount();
			if(!$result){
				$db->PDO->rollBack();
				returnAjaxJson(400, "操作失败！", null);
			}

			$db->PDO->commit();
			// 事物结束
		}

		// 排序
		public function order($data){
			global $db;
			$orderArr = $data;
			$len = count($orderArr);

			// 事务开始
			$db->PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			$db->PDO->beginTransaction();

			for($i=0; $i<$len; $i++){
				$item = $orderArr[$i];
				$listOrder = $item->listorder;
				$id = $item->id;
				// 更新数据
				$db->query("update category
							set
								listorder=:listorder
							where
								id=:id",
							array(
								"id"=>$id,
								"listorder"=>$listOrder
								)
							);
				$result = $db->prepare->rowCount();
				if(!$result){
					break;
					$db->PDO->rollBack();
					returnAjaxJson(400, "操作失败！", null);
				}
			}

			$db->PDO->commit();
			// 事物结束
		}

	}
?>